﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace LiteQuery.Controls
{
    public partial class QueryPanel : UserControl
    {
        Entities.Query query;
        public Entities.Query Query {
            get { return query; }
            set { query = value; }
        }

        public int Rows {
            get {
                return plQuery.RowCount;
            }
            set {
                if (value <= 0)
                    plQuery.RowCount = 4;
                else
                    plQuery.RowCount = value;
            }
        }

        public int Columns {
            get
            {
                return plQuery.ColumnCount;
            }
            set
            {
                if (value <= 0)
                    plQuery.ColumnCount = 4;
                else
                    plQuery.ColumnCount = value;
            }
        }

        public QueryPanel()
        {
            InitializeComponent();
        }

        public QueryPanel(Entities.Query q)
        {
            InitializeComponent();
            this.Query = q;

            this.plQuery.ColumnCount = query.Columns;
            this.plQuery.RowCount = query.Rows;
            foreach (ColumnStyle item in this.plQuery.ColumnStyles)
            {
                item.SizeType = SizeType.Percent;
                item.Width =50;
            }

            foreach (RowStyle item in this.plQuery.RowStyles)
            {
                item.SizeType = SizeType.Percent;
                item.Height = 50;
            }
        }

        private void QueryPanel_Load(object sender, EventArgs e)
        {
            InitFields();
        }

        void InitFields()
        {
            foreach (Entities.Field item in this.Query.Fields.OrderBy(x=>x.Index))
            {
                if (item.Type == "compare")
                {
                    var field = new FieldComparePanel(item)
                    {
                        Dock = DockStyle.Fill
                    };
                    plQuery.Controls.Add(field);
                }
                else if(item.Type == "normal")
                {
                    var field = new FieldPanel(item)
                    {
                        Dock = DockStyle.Fill
                    };
                    plQuery.Controls.Add(field);
                }
                else if (item.Type == "date")
                {
                    var field = new FieldDateComparePanel(item)
                    {
                        Dock = DockStyle.Fill
                    };
                    plQuery.Controls.Add(field);
                }
            }
        }
    }
}
